// 连入数据库
const connection = require('../data');

// 显示列表数据
module.exports.userFind = function (req, res) {
    connection.query('select * from users', function (error, result) {
        if (error) {
            console.log(error);
        } else {
            res.json(result);
        }
    })
}
// 添加用户
module.exports.addUser = function (req, res) {
    let data = [req.body.username, req.body.realname, req.body.email, req.body.phone, req.body.useraddress, req.body.jurisdiction, req.body.userstate];
    connection.query('insert into users(username, realname, email, phone, useraddress, jurisdiction, userstate) values(?,?,?,?,?,?,?)', data, function (error, result) {
        if (error) {
            console.log(error);
        } else {
            res.json({
                code: '200',
                msg: '添加成功'
            })

        }
    })
}
// 渲染模态框
module.exports.showDel = function (req, res) {
    let id = req.query.id;
    connection.query('select * from users where id=?',id, function (error, result) {
        if(error) {
            console.log(error);
        } else {
            res.json(result[0]);
        }
    })
}
// 提交删除
module.exports.submitDel = function (req, res) {
    connection.query('delete from users where id=?',req.query.id, function (error, result) {
        if(error) {
            console.log(error);
        } else {
            res.json({
                code: '200',
                msg: '删除成功'
            })
        }
    })
}
// 提交搜索
module.exports.search = function (req, res) {
    console.log(req.query.text);
    connection.query('select * from users where username like "%'+ req.query.text +'%"', function (error, result) {
        if (error) {
            console.log(error);
        } else {
            console.log(result);
            res.json(result);
        }
    })
}
